//
//  EagerForCareController.m
//  healthApp
//
//  Created by william mu on 12-8-29.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "EagerForCareController.h"
#import "SelfSymptomCell.h"
#import "ProtocolTranslate.h"

@interface EagerForCareController ()

@end

@implementation EagerForCareController
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"渴望关心";
    m_symptoms = nil;
    m_symptom_share_handle = nil;

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    if(m_symptoms)
        [m_symptoms release],m_symptoms = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [m_symptom_share_handle release];
    
    m_symptom_share_handle = [[NSMutableDictionary dictionary] retain];
    
    [m_allRequest release];
    m_allRequest = [[NSMutableArray array] retain];
    
    [self startRequest];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [self commitSymptomShare];
    
    ProtocolService* protocol = [ProtocolService sharedInstance];
    [protocol shutdownAnyResponse:m_allRequest];
}

-(void) startRequest
{
    GlobalState* state = [GlobalState sharedInstance];
    
    ProtocolService* protocol = [ProtocolService sharedInstance];
    
    [protocol setTarget:self];
    [protocol setAction:@selector(onGetAllSymptom:)];
    NSString* seq = [protocol getAllSymptom:state.uid];
    [m_allRequest addObject:seq];
    
}

-(void) onGetAllSymptom:(NSString*) json
{
    m_symptoms = translate_symptom(json);
    
    [m_symptoms retain];
    
    [self.tableView reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_symptoms count];
}

-(NSString*) getSymptomCodeForIndexPath:(NSIndexPath*) indexPath
{
    NSUInteger row = [indexPath row];
    NSDictionary* symptomInfo = [m_symptoms objectAtIndex:row];
    NSString* symptomCode = [symptomInfo objectForKey:@"symptomType"];
    
    return symptomCode;
}

-(NSString*) getSymptomNameForIndexPath:(NSIndexPath*) indexPath
{
    GlobalState* state = [GlobalState sharedInstance];
    
    NSString* symptomCode = [self getSymptomCodeForIndexPath:indexPath];
    NSString* symptomDesc = [state mapSymptomCode2Name:symptomCode];
    
    return symptomDesc;
}

-(BOOL) getSymptomSharedStatusForIndexPath:(NSIndexPath*) indexPath
{
    NSUInteger row = [indexPath row];
    NSString* symptomInfo = [m_symptoms objectAtIndex:row];
    NSString* shared = [symptomInfo objectForKey:@"share"];
    
    return [shared isEqual:@"1"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SelfSymptomCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"SelfSymptomCell"];
    
    if(!cell)
    {
        NSArray* ary = (NSArray*)[[NSBundle mainBundle] loadNibNamed:@"SelfSymptomCell" owner:self options:nil];
        
        cell = [ary objectAtIndex:0];
    }
    
    cell.symptomName.text = [self getSymptomNameForIndexPath:indexPath];
    cell.shareSwitch.on = [self getSymptomSharedStatusForIndexPath:indexPath];
    cell.shareSwitch.tag = [[self getSymptomCodeForIndexPath:indexPath] intValue];
    
    [cell.shareSwitch addTarget:self action:@selector(collectSymptomShare:) forControlEvents:UIControlEventValueChanged];
    
    return cell;
}

-(void) collectSymptomShare:(UISwitch*) switcher
{
    NSString* symptomType = [NSString stringWithFormat:@"%d",switcher.tag];
    
    NSString* shared = switcher.on ? @"true" : @"false";
    
    [m_symptom_share_handle setObject:shared forKey:symptomType];
}
     
-(void) commitSymptomShare
{
    ProtocolService* protocol = [ProtocolService sharedInstance];
    GlobalState* state = [GlobalState sharedInstance];
    for (NSString* symptomType in m_symptom_share_handle){
        NSString* share = [m_symptom_share_handle objectForKey:symptomType];
        NSString* seq = [protocol updateSymptomShare:state.uid symptomType:symptomType  shared:share];
        [m_allRequest addObject:seq];
    }
}

@end
